package com.keyancompany.zhongbaopingtai.activity;


import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.AppManager;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.api.UserAPI;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.User;
import com.keyancompany.zhongbaopingtai.utils.EditTextUtils;
import com.keyancompany.zhongbaopingtai.utils.IntentUtils;
import com.keyancompany.zhongbaopingtai.utils.SPUtils;
import com.keyancompany.zhongbaopingtai.utils.T;
import com.keyancompany.zhongbaopingtai.weight.CustomEditText;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by firefox on 2016/6/1.
 */
public class LoginActivity extends BaseActivity {


    @Bind(R.id.login_username)
    CustomEditText loginUsername;

    @Bind(R.id.login_password)
    CustomEditText loginPassword;

    @Bind(R.id.tx_forgetpwd)
    TextView txForgetpwd;

    @Bind(R.id.tx_register)
    TextView txRegister;

    @Bind(R.id.login_to_main)
    TextView loginToMain;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_login;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {

    }

    @Override
    void addAppManager() {
    }

    @Override
    protected void setUpViews() {
        UserAPI.logoutUser();
        loginUsername.setText((String)SPUtils.getCommData(mContext,"name",""));
        loginPassword.setText((String)SPUtils.getCommData(mContext,"pwd",""));
    }

    @OnClick({R.id.tx_forgetpwd, R.id.tx_register, R.id.login_to_main})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tx_forgetpwd:
                IntentUtils.startActivity(mContext, ForgetPwdActivity.class);
                break;
            case R.id.tx_register:
                IntentUtils.startActivity(mContext, RegisterActivity.class);
                break;
            case R.id.login_to_main:
                String userName = EditTextUtils.getEditTextValue(loginUsername);
                if ( userName == null) {
                    T.showLong(mContext, R.string.login_username_empty_hint);
                    return;
                }

                if (EditTextUtils.getEditTextValue(loginPassword) == null) {
                    T.showLong(mContext, R.string.login_password_empty_hint);
                    return;
                }

                if (isNetvaiable()) {
                    UserAPI.Login(loginUsername.getText().toString().trim(), loginPassword.getText().toString().trim(), LoginActivity.this, getLoading(R.string.watting_hint));
                }
                break;
        }
    }

    @Override
    public void success(int type, String json) {
        super.success(type, json);
        BaseModel base = validateJson(json) ;
        if (base.isSuccess()) {
            SPUtils.putCommData(mContext,"name",EditTextUtils.getEditTextValue(loginUsername));
            SPUtils.putCommData(mContext,"pwd",EditTextUtils.getEditTextValue(loginPassword));
            SPUtils.putCommData(this, Constants.IS_FROM_MAIN, true);
            SPUtils.putCommData(mContext, Constants.USER, base.data);
            SPUtils.putCommData(mContext, Constants.USER_ID, JSONObject.parseObject(base.data, User.class).getUserId());
			finish();
            IntentUtils.startActivity(mContext, MainActivity.class, null, null, Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        }
    }

    @Override
    public void failed(String error) {
        super.failed(error);
        T.showShort(mContext, error);
    }

    @Override
    void removeAppManager() {
    }
}
